package com.atguigu.yygh.cmn.controller;


import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.model.cmn.Dict;
import com.atguigu.yygh.vo.cmn.DictEeVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 组织架构表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-11-03
 */
@Api(value = "数据字典接口")
@RestController
@RequestMapping("/admin/dict")
public class DictController {
    @Autowired
    private DictService dictService;

    /**
     * @Description 根据id获取数据字典信息
     * @param id:
     * @return: com.atguigu.yygh.common.R.R
     */
    @GetMapping("/findChildData/{id}")
    public R findChildData(@PathVariable Long id){
        List<Dict> dictList = dictService.findChildData(id);
        return R.ok().data("dictList",dictList);
    }

    @GetMapping("/downLoad")
    public void downLoad(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("数据字典", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        dictService.downLoad(response);
    }

    @PostMapping("/upLoad")
    public R upLoad(MultipartFile file) throws IOException {
        dictService.upLoad(file);
        return R.ok();
    }

    @GetMapping("/name/{parentDictCode}/{value}")
    public String getName(@PathVariable("parentDictCode") String parentDictCode,@PathVariable("value") Long value){
        return dictService.getName(parentDictCode,value);
    }

    @GetMapping("/name/{value}")
    public String getName(@PathVariable("value") Long value){
        return dictService.getName("",value);
    }

    @GetMapping("/findProvince/{province}")
    public R findProvince(@PathVariable String province){
        List<Dict> dictList = dictService.findProvince(province);
        return R.ok().data("items",dictList);
    }

    @GetMapping("/findCity/{parentId}")
    public R findCity(@PathVariable Long parentId){
        List<Dict> dictList = dictService.findCity(parentId);
        return R.ok().data("items",dictList);
    }

    @GetMapping("/findByDictCode/{dictCode}")
    public R findByDictCode(@PathVariable String dictCode){
        List<Map<String,String>> hostypeList = dictService.findByDictCode(dictCode);
        return R.ok().data("list",hostypeList);
    }

    @GetMapping("/findChildDataByParentId/{parentId}")
    public R findChildDataByParentId(@PathVariable Long parentId){
        List<DictEeVo> childData = dictService.findChildDataByParentId(parentId);
        return R.ok().data("list",childData);
    }
}

